package com.cynteka.cyfoman.ui;

import com.cynteka.cyfoman.core.config.ColumnMD;
import com.cynteka.cyfoman.core.config.QueryMD;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * User: Sergey
 * Date: 24.07.11
 * Time: 16:50
 */
public class DBTableModel extends AbstractTableModel implements QueryTableModel {
    private QueryMD queryMD;
    private DataSource dataSource;

    private List<Map<String, Object>> list;
    private List<String> columns;

    public DBTableModel() {
    }

    public QueryMD getQuery() {
        return queryMD;
    }

    public void setQuery(QueryMD queryMD) {
        this.queryMD = queryMD;
        refresh();
    }

    @Override
    public Object getValue(int row, String fieldName) {
        Map<String, Object> map = list.get(row);
        return map.get(fieldName);
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        List<ColumnMD> qColumns = queryMD.getColumns();
        if (qColumns != null) {
            return qColumns.size();
        } else {
            return list.size() > 0 ? list.get(0).size() : 0;
        }
    }

    @Override
    public String getColumnName(int i) {
        String res;
        List<ColumnMD> qColumns = queryMD.getColumns();
        if (qColumns != null) {
            ColumnMD qColumn = qColumns.get(i);
            res = qColumn.getTitle();
        } else {
            res = columns.get(i);
        }
        return res;
    }

    @Override
    public Class<?> getColumnClass(int i) {
        return String.class;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean isCellEditable(int i, int i1) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Object getValueAt(int row, int col) {
        List<ColumnMD> qColumns = queryMD.getColumns();
        Map<String, Object> map = list.get(row);
        Object res;
        if (qColumns != null) {
            ColumnMD qColumn = qColumns.get(col);
            res = map.get(qColumn.getName());
        } else {
            res = list.get(row).get(columns.get(col));
        }

        return res;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        refresh();
    }

    public void refresh() {
        if (dataSource != null && queryMD != null) {
            JdbcTemplate template = new JdbcTemplate(dataSource);
            list = template.queryForList(queryMD.getSql());
            columns = list.size() > 0 ? new ArrayList<String>(list.get(0).keySet()) : null;
        }
    }
}
